﻿using AutoMapper;
using B.S.Data.MES.API.Applications.ProcessmaintenanceApplications.Command;
using B.S.Data.MES.API.Dto.DTOProcess;
using B.S.Domain.Process.ProcessmaintenanceModel;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Data.MES.API.Applications.ProcessmaintenanceApplications.Horter
{
    public class ShowProcessmaintenanceCommandHorter : IRequestHandler<ShowProcessmaintenanceCommand, APIPageing<List<DtoProcessmaintenance>>>
    {
        IMapper mediator;
        IBaseRepository<Processmaintenance> processmaintenanceReq;

        public ShowProcessmaintenanceCommandHorter(IMapper mediator, IBaseRepository<Processmaintenance> processmaintenanceReq)
        {
            this.mediator = mediator;
            this.processmaintenanceReq = processmaintenanceReq;
        }

        public async Task<APIPageing<List<DtoProcessmaintenance>>> Handle(ShowProcessmaintenanceCommand request, CancellationToken cancellationToken)
        {
            var res = new APIPageing<List<DtoProcessmaintenance>>();
            
            // 使用IQueryable进行查询，不立即执行
            var query = processmaintenanceReq.GetAll().Where(o=>o.IsDel==false);

            // 添加过滤条件（如果需要）
            if (!string.IsNullOrEmpty(request.ProcessmaintenanceEncode))
            {
                query = query.Where(x => x.ProcessmaintenanceEncode.Contains(request.ProcessmaintenanceEncode));
            }
            if (!string.IsNullOrEmpty(request.ProcessmaintenanceName))
            {
                query = query.Where(x => x.ProcessmaintenanceName.Contains(request.ProcessmaintenanceName));
            }
            if (!string.IsNullOrEmpty(request.ProcessmaintenanceType))
            {
                query = query.Where(x => x.ProcessmaintenanceType.Contains(request.ProcessmaintenanceType));
            }
            //if (request.ProcessmaintenanceReportingmode!=null)
            //{
            //    query = query.Where(x => x.ProcessmaintenanceReportingmode==request.ProcessmaintenanceReportingmode);
            //}
            //if (!string.IsNullOrEmpty(request.ProcessmaintenanceDescription))
            //{
            //    query = query.Where(x => x.ProcessmaintenanceDescription.Contains(request.ProcessmaintenanceDescription));
            //}
            //if (!string.IsNullOrEmpty(request.ProcessmaintenanceCreatorName))
            //{
            //    query = query.Where(x => x.ProcessmaintenanceCreatorName.Contains(request.ProcessmaintenanceCreatorName));
            //}
            //if (request.ProcessmaintenanceCreatorTimeKK != null&&request.ProcessmaintenanceCreatorTimeJJ!=null)
            //{
            //    query=query.Where(x => x.ProcessmaintenanceCreatorTime >= request.ProcessmaintenanceCreatorTimeKK && x.ProcessmaintenanceCreatorTime <= request.ProcessmaintenanceCreatorTimeJJ);
            //}
            //if (request.ProcessmaintenanceUpdateTimeKK != null && request.ProcessmaintenanceUpdateTimeJJ != null)
            //{
            //    query = query.Where(x => x.ProcessmaintenanceUpdateTime >= request.ProcessmaintenanceUpdateTimeKK && x.ProcessmaintenanceUpdateTime <= request.ProcessmaintenanceUpdateTimeJJ);
            //}
            // 异步获取总记录数
            res.TotalCount = await query.CountAsync(cancellationToken);
            res.Total = (int)Math.Ceiling(res.TotalCount * 1.0 / request.PageSize);
            
            // 异步执行分页查询
            var pagelist = await query.OrderByDescending(o => o.Id)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize).ToListAsync();

            var sou = mediator.Map<List<DtoProcessmaintenance>>(pagelist);
            res.Data = sou;
            //res.Code = (int)RequstCode.成功;
           // res.msg = "显示成功";
            return res;
        }
    }
}
